package com.supermap.client;
/**
 * @author oupp
 * @date 2021/4/25 10:44
 */

import com.supermap.SM2Utils;
import com.supermap.crypto.CryproType;
import com.supermap.crypto.CryptoUtils;
import com.supermap.protocol.RpcRequest;
import io.netty.channel.Channel;

import java.io.File;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.cert.X509Certificate;
import java.util.logging.Logger;

/**
 * 记录一些公共变量
 */
class NettyClientUtils {

    static CryproType cryproType;
    private static Logger logger = Logger.getLogger(NettyClient.class.toString());

    static PrivateKey clientPrivateKey;
    static PublicKey clientPublicKey;
    static PrivateKey serverPrivateKey;
    static PublicKey serverPublicKey;
    static CryptoUtils cryptoUtils = new CryptoUtils();


    static  CryproType getCryproType(){
        return cryproType;
    }

    static byte[] encrypt(String msg) {
        if (cryproType == CryproType.RSA) {
            try {
                return cryptoUtils.rsaEncrypt(msg, serverPublicKey);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (cryproType == CryproType.NOCRY) {
            try {
                return msg.getBytes("utf-8");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }


    static String decrypt(byte[] msg) {

        if (cryproType == CryproType.RSA) {
            try {
                return cryptoUtils.rsaDecrypt(msg, clientPrivateKey);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    static void loadServerPrivFromPath(String filepath) {
        try {
            File file = new File(filepath);
            if (file.exists()) {
                serverPrivateKey = SM2Utils.loadPrivFromFile(filepath, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static void loadServerPublicKey(PublicKey publicKey) {
        serverPublicKey = publicKey;
    }


    static void loadServerPublicFromCertificate(X509Certificate x509Certificate) {
        serverPublicKey = x509Certificate.getPublicKey();
    }

    static void generateClientKeyPair(int length){

        if (cryproType == CryproType.RSA) {
            cryptoUtils.generatekeys(cryproType, length);
            clientPrivateKey = cryptoUtils.getRsaPrivateKey();
            clientPublicKey = cryptoUtils.getRsaPublicKey();
        }
    }

    static PublicKey getClientPublicKey() {
        if (cryproType == CryproType.NOCRY) {
            return null;
        }
        return clientPublicKey;
    }

    /**
     * 发送信息获取 Server 公钥
     * @param channel
     */
    static void getServerPublickey(Channel channel, String serverHost, int serverPort){
        try {
            if (cryproType == CryproType.RSA) {
                RpcRequest request = new RpcRequest();
                request.setPublicKey(CryptoUtils.RSAPubToString(clientPublicKey));
                request.setIpfsIp(serverHost);
                request.setIpfsPort(String.valueOf(serverPort));
                request.setInfoPurposes("getServerPublickey");
                channel.writeAndFlush(request);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
